var gulp = require('gulp');
var watch = require('gulp-watch');
var nodeSass = require('gulp-sass');
// var rename = require('gulp-rename');
var babel = require('gulp-babel');
// var minify = require('gulp-minify-css');
// var uglify = require('gulp-uglify');
var es2015Presets = require('babel-preset-es2015');

gulp.task('nodeSass', function() {
    gulp.src('../.scss/**/*.scss')
        .pipe(nodeSass({    //  https://github.com/sass/node-sass#options
            outputStyle: 'expanded',  // nested, expanded, compact, compressed
            sourceComments:true
        }))
        .on('error',handleErr) // 报错防止中断
        .pipe(gulp.dest('../css'))
        // .pipe(rename({suffix:'.min'}))
        // .pipe(minify())
        .on('error',handleErr) // 报错防止中断
        .pipe(gulp.dest('../css'));
        console.log("scss change");
});


gulp.task('6to5', function() {
    return gulp.src('../.js/**/*.js')
        .pipe(babel({
            presets: es2015Presets
        }))       
        .pipe(rename({
            suffix: '.min'
        }))
        .pipe(gulp.dest('../js'));
});
gulp.task('watch:6to5', function() {
    gulp.watch('../.js/**/*.js', ['6to5'])
});



// 处理错误信息，防止中断server
function handleErr(err){
    console.error(err)
    this.emit('end');
}

// uglify 压缩 保留注释规则
function keepHeaderComments(node, comment) {
    var text = comment.value;
    var type = comment.type;
    if (type == "comment2") {// multiline comment
        return true
    }
}

// 手机端vue 组件6转6
gulp.task('mobile6to5', function() {
    return gulp.src('../.js/mobile/vueComponent/*.js')
        .pipe(babel({
            presets: es2015Presets
        }))
        .on('error',handleErr) // 报错防止中断
        .pipe(gulp.dest('../js/mobile/vueComponent'))        
        .pipe(uglify({
            output:{comments:keepHeaderComments}
        }))               
        .pipe(rename({
            suffix: '.min'
        }))
        .pipe(gulp.dest('../js/mobile/vueComponent'));
});


// js输出路径
function jsOutput(eventPath){
    var outputPath =  '../js';
    if(eventPath.search("pcjs") != -1){
        outputPath= '../js/pcjs'
    }
    if(eventPath.search("mobilejs") != -1){
        outputPath= '../js/mobilejs'
    }
    if(eventPath.search("vueComponentjs") != -1){
        outputPath= '../js/vueComponentjs'
    }
    return outputPath
}

// css 输出路径
function styleOutput(eventPath){
    var outputPath =  '../css';  
    return outputPath
}


// 手机端vue 组件6转6
gulp.task('watch:mobile6to5', function() {
    gulp.watch('src/*.js', function(event){
        gulp.src(event.path)
        .pipe(babel({
            presets: es2015Presets
        }))
        .on('error',handleErr) // 报错防止中断
        .pipe(gulp.dest("./build") ) ;     
        // .pipe(uglify({
        //     output:{comments:keepHeaderComments}
        // }))               
        // .pipe(rename({
        //     suffix: '.min'
        // }))
        // .pipe( gulp.dest(jsOutput(event.path)) );

        var charIndex =  event.path.search('.js');
        var subPath = event.path.substr(charIndex);
        console.log("js "+subPath+" has been "+event.type);
    })
});


gulp.task('watch:sass', function() {
    gulp.watch('src/*.scss', function(event) {        
        gulp.src(event.path)
        .pipe(nodeSass({    //  https://github.com/sass/node-sass#options
            outputStyle: 'nested',  // nested, expanded, compact, compressed
            sourceComments:true  // 保留行号注释，方便调试
        }))
        .on('error', function(err){ // 报错防止中断
            console.error(err)
            this.emit('end');
        })
        .pipe(gulp.dest('./build'));
        // .pipe(nodeSass({    //  https://github.com/sass/node-sass#options
        //     outputStyle: 'compressed',  // nested, expanded, compact, compressed（压缩）                
        // }))
        // .pipe(rename({suffix:'.min'}))
        // .pipe(gulp.dest(styleOutput(event.path)));

        var charIndex =  event.path.search('.scss');
        var subPath = event.path.substr(charIndex);
        console.log("scss "+subPath+" has been "+event.type);
    })
});

// gulp.task('watch', ['watch:sass']);
gulp.task('watch', ['watch:sass', 'watch:mobile6to5']);
// gulp.task('watch', ['watch:6to5']);

gulp.task('default', function() {});